﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class QuanLyPhanQuyenBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<CT_ROLE> layTatCaCTRole()
        {
            return DB.CT_ROLEs.ToList();
        }
        public List<ROLE> layTatCaRole()
        {
            return DB.ROLEs.ToList();
        }
        public ROLE layCTRoleTheoID(int _MaRole) 
        {
            ROLE r = DB.ROLEs.SingleOrDefault(a => a.MaRole == _MaRole);
            return r;
        }
        //Kiem tra role ton tai
        public string KiemTraTonTai(string _TenRole)// Neu ma role tu dong tang thi khong can kiem tra ma role
        {
            if (DB.ROLEs.Any(b => b.TenRole == _TenRole))
                return "Tên Phòng Ban";
            else
                return null;
        }
        //public string KiemTraTonTai(int _MaRole, string _TenRole)
        //{
        //    if (DB.ROLEs.Any(a => a.MaRole == _MaRole))
        //        return "Mã Phòng Ban";
        //    if (DB.ROLEs.Any(b => b.TenRole == _TenRole))
        //        return "Tên Phòng Ban";
        //    else
        //        return null;
        //}

        //Sua role

        //Xoa role
        public void XoaRole(int _MaRole)
        {
            ROLE r = DB.ROLEs.Single(a => a.MaRole == _MaRole && a.Xoa == 1);
            DB.ROLEs.DeleteOnSubmit(r);
            DB.SubmitChanges();
        }

        public void themRole(ROLE r)
        {
            DB.ROLEs.InsertOnSubmit(r);
            DB.SubmitChanges();
        }

        public void suaRole(ROLE r)
        {
            ROLE rr = DB.ROLEs.SingleOrDefault(a => a.MaRole == r.MaRole);
            rr.TenRole = r.TenRole;
            DB.SubmitChanges();
        }
        
        //Them user vao role

        public void themTaiKhoanVaoRole(CT_ROLE r)
        {
            DB.CT_ROLEs.InsertOnSubmit(r);
            DB.SubmitChanges();
        }
        public List<int> LayMaRoleTuNV(int _MaNV)
        {
            List<int> r = (from a in DB.CT_ROLEs
                            where a.MaNV == _MaNV
                            select a.ROLE.MaRole).ToList();
            return r;
        }
        public void XoaRoleNV(int _MaNV)
        {
            List<CT_ROLE> lst = DB.CT_ROLEs.Where(a => a.MaNV == _MaNV).ToList();
            DB.CT_ROLEs.DeleteAllOnSubmit(lst);
            DB.SubmitChanges();
        }
    }
}
